<template >
  <div class="bb">
    <template>
      <div class="mymapM">
        <!--捜索-->
        <!-- <div v-if="loading" class="loading">
          <van-loading type="spinner" />
        </div>-->

        <div class="search-box">
          <div class="box-jj">
            <div>
              <span class="sap">发布代驾</span>
            </div>
            <div>
              <p @click="zhaoqu">
                <span class="sap" badge="12">
                  <!-- <van-sidebar-item title="代驾挣钱" badge="99+" /> -->
                  代驾挣钱
                  <van-badge
                    style="margin-left: -10px;"
                    v-show="namas"
                    :content="$store.state.plugins.length"
                    max="99"
                  />
                </span>
              </p>
            </div>
          </div>
          <div class="box-tramp">
            <router-link style="display: flex;" tag="div" to="/dir">
              <img src="../assets/png/上车点.png" width="65" alt />
            </router-link>
          </div>
          <div class="search-postion" style="display:flex;flex-wrap:wrap;">
            <!-- <span class="buts">返回</span>
            <input type="text" placeholder="输入关键字搜索" v-model="search_key" />
            <span class="clear" v-if="search_key" @click="search_key ='' ">
              <van-icon color="#8f8f8f" name="clear" />
            </span>
            <span class="buts border_but" @click="keySearch()">捜索</span>-->

            <van-steps
              direction="vertical"
              active-icon="location"
              inactive-icon="smile"
              :active="0"
              style="width:85%;height:140px;"
            >
              <van-step>
                <br />
                <!-- <p class="box-pp" >正在获取位置信息...</p> -->
                <van-search
                  style="width:120%;margin-top: -10px;"
                  v-model="destin2"
                  background="#fff"
                  placeholder="请选择目的地"
                  @click="$router.push('/todo')"
                />
              </van-step>
              <van-step>
                <van-search
                  style="width:120%;margin-top: -10px;"
                  v-model="destin"
                  background="#fff"
                  placeholder="请输入目的地"
                />
              </van-step>
              <van-step></van-step>
            </van-steps>

            <van-button @click="showPopup" type="primary" size="large">下一步</van-button>
          </div>
        </div>
        <van-popup
          v-model="show"
          closeable
          close-icon-position="top-right"
          position="bottom"
          :style="{ height: '35%' }"
        >
          <div class="box-fuqia">￥38.0</div>
          <div class="box-xuan">
            <p style="margin-left:30px;">选择支付方式</p>
            <div class="box-zhifu">
              <ul>
                <li>
                  <img src="../assets/png/余额.png" alt />
                  <span>余额支付</span>
                </li>
                <li>
                  <img src="../assets/png/微信.png" alt />
                  <span>微信支付</span>
                </li>
                <li>
                  <img src="../assets/png/支付宝.png" alt />
                  <span style="margin-left:-15px;">支付宝支付</span>
                </li>
              </ul>
              <van-radio-group v-model="radio">
                <van-radio style="height:50px;margin-right:15px" icon-size="30px" name="1"></van-radio>
                <van-radio style="height:50px;margin-right:15px" icon-size="30px" name="2"></van-radio>
                <van-radio style="height:50px;margin-right:15px" icon-size="30px" name="3"></van-radio>
              </van-radio-group>
            </div>
            <van-button @click="succes" type="primary" block>确认支付</van-button>
          </div>
        </van-popup>
        <div class="con-box con-map" v-if="!search_key">
          <!--地图-->
          <div class="mapbox">
            <div class="map" id="container"></div>
            <div class="sign"></div>
          </div>
        </div>
        <div class="con-box" v-if="!search_key">
          <!--地址列表-->
          <div class="Hlist-box">
            <ul>
              <li v-for="(item, index) in lists" :key="index" @click="onAddressLi(item)">
                <b>
                  <van-icon color="#a6a6a6" name="clock" />
                </b>
                <div>
                  <span>{{item.name}}</span>
                  <p>{{item.address}}</p>
                </div>
              </li>
            </ul>
          </div>
        </div>
        <!--捜索列表-->
        <div class="search-list" v-if="search_key">
          <ul>
            <li
              v-for="(item, index) in search_list"
              :key="index"
              @click="onSearchLi(item.location)"
            >
              <span>{{item.name}}</span>
              <p>{{item.address}}</p>
            </li>
            <li v-if="noSearchShow">
              <p>暂无搜索结果</p>
            </li>
          </ul>
        </div>
      </div>
    </template>
  </div>
</template>
<script>
import { Toast } from "vant";
import { Dialog } from "vant";
export default {
  data() {
    return {
      center: [116.42792, 39.902896], //经度+纬度
      search_key: "", //搜索值
      lists: [], //地点列表
      search_list: [], //搜索结果列表
      marker: "",
      loading: false,
      noSearchShow: false, //无搜索结果提示，无搜索结果时会显示暂无搜索结果
      show: false,
      radio: "1",
      destin: "",
      destin2: "",
      data: {},
      namas: false
    };
  },
  mounted() {
    setTimeout(() => {
      this.adMap();
    }, 1000);
    this.destin2 = this.$store.state.addrs;
    if (this.$store.state.names != "") {
      this.namas = true;
    } else {
      this.namas = false;
    }
  },
  methods: {
    succes() {
      if (this.radio === "1") {
        const _this = this;
        function beforeClose(action, close) {
          if (action === "confirm") {
            setTimeout(close, 1000);
            setTimeout(function() {
              _this.$router.push("/pay");
            }, 1500);
          } else {
            Dialog.close();
          }
        }
        Dialog.confirm({
          title: "余额支付",
          message: "确认支付吗？",
          beforeClose
        });
      } else if (this.radio === "2") {
        this.$router.push("/many");
      } else {
        this.$router.push("/zhi");
      }
    },
    showPopup() {
      if (this.destin.trim() === "" || this.destin2.trim() === "") {
        Toast.fail("请输入目的地");
        this.show = false;
      } else {
        this.show = true;
        this.$store.commit("setwhat", this.destin);
        // 时间
        var mydate = new Date();
        var year = mydate.getFullYear();
        var month = mydate.getMonth() + 1;
        var day = mydate.getDate();
        var hour = mydate.getHours();
        var minutes = mydate.getMinutes();
        if (minutes < 10) {
          minutes = "0" + minutes;
        }
        var seconds = mydate.getSeconds();
        if (seconds < 10) {
          seconds = "0" + seconds;
        }
        var weekday = mydate.getDay();
        var arr = new Array("", "", "", "", "", "", "");
        var time =
          " " +
          year +
          "-" +
          month +
          "-" +
          day +
          " " +
          hour +
          ":" +
          minutes +
          ":" +
          seconds +
          " " +
          arr[weekday];
        // this.times = time;
        this.$store.commit("settimer", time);
        const code = Math.floor(Math.random() * 1000000)
          .toString()
          .padStart(5, "0");
        this.$store.commit("setrand", code);
        // 提交信息
        this.data.timer = time;
        this.data.rand = code;
        this.data.what = this.destin;
        this.data.addrs = this.destin2;
        this.$store.commit("setDatas", this.data);
        this.$store.commit("setplugins", this.data);
        this.data = {};
        this.destin = "";
      }
    },
    zhaoqu() {
      if (this.$store.state.names != "") {
        Toast.success("获取成功");
        this.$router.push("/qd");
        // console.log(this.$store.state.datas.length);
      } else {
        Toast.fail("先注册吧");
        this.$router.push("/zhao");
      }
    },
    adMap() {
      this.loading = true;
      //初始化地图
      var map = new AMap.Map("container", {
        zoom: 14, //缩放级别
        center: this.center //设置地图中心点
        //resizeEnable: true,  //地图初始化加载定位到当前城市
      });
      //获取初始中心点并赋值
      var currentCenter = map.getCenter(); //此方法是获取当前地图的中心点
      this.center = [currentCenter.lng, currentCenter.lat]; //将获取到的中心点的纬度经度赋值给data的center
      // console.log(this.center);

      //创建标记
      this.marker = new AMap.Marker({
        position: new AMap.LngLat(currentCenter.lng, currentCenter.lat) // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
      });
      // 将创建的点标记添加到已有的地图实例：
      map.add(this.marker);

      //根据地图中心点查附近地点，此方法在下方
      this.centerSearch();
      //监听地图移动事件，并在移动结束后获取地图中心点并更新地点列表
      var moveendFun = e => {
        // 获取地图中心点
        currentCenter = map.getCenter();
        this.center = [currentCenter.lng, currentCenter.lat];
        this.marker.setPosition([currentCenter.lng, currentCenter.lat]); //更新标记的位置
        //根据地图中心点查附近地点
      };
      //更新数据
      var centerSearch = () => {
        this.loading = true;
        this.centerSearch();
      };

      // 绑定事件移动地图事件
      map.on("mapmove", moveendFun); //更新标记
      map.on("moveend", centerSearch); //更新数据
    },
    centerSearch() {
      AMap.service(["AMap.PlaceSearch"], () => {
        //构造地点查询类
        var placeSearch = new AMap.PlaceSearch({
          type:
            "汽车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|地名地址信息", // 兴趣点类别
          pageSize: 30, // 单页显示结果条数
          pageIndex: 1, // 页码
          city: "全国", // 兴趣点城市
          autoFitView: false // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
        });
        //根据地图中心点查附近地点
        placeSearch.searchNearBy(
          "",
          [this.center[0], this.center[1]],
          200,
          (status, result) => {
            if (status == "complete") {
              this.lists = result.poiList.pois; //将查询到的地点赋值
              this.loading = false;
            }
          }
        );
      });
    },
    keySearch() {
      this.loading = true;
      AMap.service(["AMap.PlaceSearch"], () => {
        //构造地点查询类
        var placeSearch = new AMap.PlaceSearch({
          type:
            "汽车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|地名地址信息", // 兴趣点类别
          pageSize: 30, // 单页显示结果条数
          pageIndex: 1, // 页码
          city: "全国", // 兴趣点城市
          citylimit: false, //是否强制限制在设置的城市内搜索
          autoFitView: false // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
        });
        //关键字查询
        placeSearch.search(this.search_key, (status, result) => {
          if (status == "complete") {
            if (result.poiList.count === 0) {
              this.noSearchShow = true;
            } else {
              this.search_list = result.poiList.pois; //将查询到的地点赋值
              this.noSearchShow = false;
              this.loading = false;
            }
          } else {
            this.search_list = [];
            this.noSearchShow = true;
          }
        });
      });
    },
    onAddressLi(e) {
      console.log(e);
      this.marker.setPosition([e.location.lng, e.location.lat]); //更新标记的位置
    },
    onSearchLi(e) {
      console.log(e.lng + "-" + e.lat);
      this.center = [e.lng, e.lat];
      console.log(this.center);
      this.search_key = "";
      // this.loading=true;
      setTimeout(() => {
        this.adMap();
      }, 1000);
    }
  },
  watch: {
    search_key(newv, oldv) {
      if (newv == "") {
        this.search_list = [];
        this.noSearchShow = false;
        setTimeout(() => {
          this.adMap();
        }, 1000);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.con-map {
  height: 180px;
  width: 100%;
}
.con-box {
  .mapbox {
    margin-bottom: 10px;
    position: fixed;
    z-index: 111;
    width: 100%;
    height: 100%;
    background: #eceeee;

    .map {
      width: 100%;
      height: 100%;
    }
  }

  .Hlist-box {
    width: 96%;
    margin: 0 auto;

    background: #fff;
    border-radius: 5px;
    li {
      height: 40px;
      padding: 14px 22px;
      border-bottom: 1px solid #d9d9d9;
      display: flex;
      b {
        display: inline-block;

        i {
          margin: 18px 18px 0 0;
        }
      }
      div {
        width: 100%;
      }
      span {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        font-size: 15px;
        display: inline-block;
        width: 90%;
      }
      p {
        margin-top: 10px;
        color: #a6a6a6;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        font-size: 13px;
        width: 90%;
      }
    }
  }
}
.mymapM {
  .search-box {
    height: 48px;
    line-height: 48px;
    background: rgb(243, 241, 241);
    border-bottom: 1px solid #bfbec4;
    font-family: "Arial", "Microsoft YaHei", "黑体", "宋体", sans-serif;
    .box-jj {
      width: 100%;
      height: 48px;
      background: #fff;
      display: flex;
      justify-content: space-around;
    }
    .sap {
      border-bottom: 3px solid black;
      padding-bottom: 10px;
    }
    .search-postion {
      height: 200px;
      line-height: 48px;
      background: #fff;
      border-bottom: 1px solid #bfbec4;
      width: 90%;
      position: fixed;
      z-index: 999;
      display: flex;
      bottom: 10px;
      left: 5%;
      input {
        flex: 4;
        height: 14px;
        padding: 16px 0;
        border: none;

        text-indent: 10px;
      }
      .clear {
        margin: 2px 6px;
      }
      .buts {
        width: 15%;
        text-align: center;
        display: inline-block;
        flex: 1;
      }
      .border_but {
        border-left: 1px solid #8f8f8f;
        height: 14px;
        line-height: 14px;
        margin: 17px 0;
      }
    }
  }
  .search-list {
    width: 96%;
    margin: 0 auto;
    margin-top: 10px;
    border-radius: 5px;
    background: #fff;
    li {
      height: 40px;
      padding: 14px 22px;
      border-bottom: 1px solid #d9d9d9;
      span {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        font-size: 15px;
        display: inline-block;
        width: 90%;
      }
      p {
        margin-top: 10px;
        color: #a6a6a6;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        font-size: 13px;
        width: 90%;
      }
    }
  }
}
.loading {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 99999999;
}
.box-pp {
  width: 100%;
  height: 35px;
  margin-left: 11px;
  background: #f7f8fa;
  line-height: 35px;
  text-indent: 13px;
}
.box-fuqia {
  width: 100%;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-size: 20px;
}
.box-xuan {
  width: 100%;
  height: 160px;
}
.box-zhifu {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-between;
  ul {
    width: 100%;
  }
  li {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid #eee;
    display: flex;
    justify-content: space-around;
    align-items: center;
    img {
      width: 35px;
      margin-left: -40px;
    }
  }
}
.box-tramp {
  width: 50px;
  height: 50px;
  border: 1px solid red;
  position: absolute;
  z-index: 555;
  bottom: 50%;
  right: 10px;
  border-radius: 50px;
  background: antiquewhite;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>